package com.yeyks.controller.common;

import cn.hutool.core.lang.Validator;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.utils.JWTUtil;
import com.yeyks.commonReference.append.mongodb.service.CnRoomImageMongoMongoService;
import com.yeyks.commonReference.append.other.ImageCodeService;
import com.yeyks.commonReference.service.consumer.MemberCardService;
import com.yeyks.rocketmq.service.customer.CommonDisposeService;
import com.yeyks.rocketmq.service.customer.MiniCardPaySuccessListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "测试接口")
//@ApiIgnore
@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {
    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private CommonDisposeService commonDisposeService;
    @Autowired
    private ImageCodeService imageCodeService;
    @Autowired
    private MiniCardPaySuccessListener listener;
    @Autowired
    private CnRoomImageMongoMongoService cnRoomImageMongoMongoService;
    private static Integer count = 1;

    @ApiIgnore
    @ApiOperation("测试 --> 生成会员卡")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "count", value = "生成的数量", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "cardType", value = "卡类型(1会员卡,2精选)", required = true, dataType = "int"), @ApiImplicitParam(paramType = "query", name = "givenAmount", value = "赠送余额", required = true, dataType = "int"), @ApiImplicitParam(paramType = "query", name = "saleType", value = "销售类型(1线下,2线上)", required = true, dataType = "int"), @ApiImplicitParam(paramType = "query", name = "salePrice", value = "售价", required = true, dataType = "int")
    })
    @PostMapping("/add/card")
    public AjaxResponseBody addCard(Integer count, Integer cardType, Integer givenAmount, Integer saleType, Integer salePrice) {
        memberCardService.createMemberCard(count, cardType, givenAmount, saleType, salePrice);
        return AjaxResponseBody.success(true);
    }

    @ApiIgnore
    @ApiOperation("测试 --> 监听websocket")
    @PostMapping("/listen/websocket")
    public AjaxResponseBody listenWebsocket() {
        commonDisposeService.pushMessage("1192027662990704640");
        return AjaxResponseBody.success(true);
    }

    @ApiIgnore
    @ApiOperation("测试 --> 算术验证码")
    @GetMapping("/operator/code")
    public void operatorCode(String phone, HttpServletResponse response) {
        Arguments.strIsNull(phone, "手机号不能为空");
        boolean mobile = Validator.isMobile(phone);
        Arguments.isFalse(mobile, "手机号格式有误");
        imageCodeService.getCode("66", phone, response);
    }

    @ApiIgnore
    @ApiOperation("测试 --> 校验算术验证码是否正确")
    @GetMapping("/verify/operation/code")
    public AjaxResponseBody<Boolean> verifyOperationCode(String phone, String code) {
        return AjaxResponseBody.success(imageCodeService.verify("66", phone, code));
    }

    @ApiOperation(value = "v4.3 --> 获取token用来测试", notes = "不填默认总公司")
    @GetMapping("/get/token")
    public AjaxResponseBody<String> getToken(Integer id) {
        if (null == id) {
            id = 1403;
        }
        return AjaxResponseBody.success(JWTUtil.createToken(String.valueOf(id)));
    }

    @ApiIgnore
    @ApiOperation(value = "测试 --> mongo房间数据修改")
    @GetMapping("/mongo")
    public AjaxResponseBody mongo() {
        if (1 == count) {
            if (cnRoomImageMongoMongoService.updateErrorData()) {
                count++;
            }
        }
        return AjaxResponseBody.success();
    }

    @ApiOperation(value = "v4.1 --> 卖卡佣金入账")
    @GetMapping("/sell/card/order")
    public AjaxResponseBody sellCardOrder(String orderNo) {
        Arguments.strIsNull(orderNo, "订单号不能为空");
        listener.testSellCardOrder(orderNo);
        return AjaxResponseBody.success();
    }
}
